<!-- 添加发票 -->
<template>
    <div>
        <div class="f_i">
            <n-upload :action='prefixS + "public/Upimg/upodf"' multiple @before-upload="loading = true" :data="upload"
                accept=".ofd" @finish="handleFinish">
                <n-button :loading="loading">点击上传odf文件</n-button>
            </n-upload>
        </div>
        <div class="f_i ml10">
            <n-button type="primary" size="medium" @click="daochu"
                :disabled="pitchOn.length > 0 ? false : true" :loading="loadinhg">导出</n-button>

        </div>
        <!-- {{ pitchOn }} -->
        <div class="mt10 z9 fz12 sdfsdfdsrtrt">
            <transferFormat ref="transferFormatref" @zhsuccess="zhsuccess"></transferFormat>
        </div>

    </div>
</template>
<script lang='ts' setup>
import { da } from "date-fns/locale";
import { ref, reactive } from "vue"
import { dxget, prefixS, download, callBack, dxpost, qurl } from "../../util/index";
import transferFormat from "./components/transferFormat.vue"
const loading = ref(false)
const transferFormatref = ref()
const texq = ref()
const apiurl = 'fapiao/fInvoiceList'
const fileName = ref()
const loadinhg = ref(false)
const props = defineProps(['pitchOn'])
const emit = defineEmits(["callbaclk"]);
let jkjknn = []
const handleFinish = async ({
    file,
    event
}) => {
    const data = JSON.parse(event.currentTarget.response)
    if (data.data.imgurl) {
        transferFormatref.value.odfzpdf(data.data.imgurl)
    }
    loading.value = false
    fileName.value = data.data.fileName
    console.log(fileName.value);
    jkjknn.push(data.data.fileName)
};
let upload = reactive({
    typeName: "",
});
// 上传odf文件成功的回调
let jjhnse = 0
const zhsuccess = async (data) => {
    texq.value = data
    fileName.value = jkjknn[jjhnse++]
    await addFp(data)
    emit("callbaclk")
}

// 添加发票
const addFp = async (te) => {
    const texerw = JSON.parse(JSON.stringify(te))
    texerw.ProjectList = JSON.stringify(texerw.ProjectList)
    const { data }: any = await dxpost(apiurl, {
        ...texerw,
        fileName: fileName.value
    })
}

const daochu = async () => {
  loadinhg.value = true
    const { data }: any = await dxget("fapiao/api/getpdfList", { id: props.pitchOn.join(",") })
    for (let i = 0; i < data.length; i++) {
        if (data[i].pdfUrl) {
           await  download(data[i].pdfUrl, data[i].fileName)
        } else {
            const jhjse = encodeURIComponent(import.meta.env.VITE_APIURL + 'fapiao/invoice?id=' + data[i].id)
            const pfds: any = await dxget("https://duxinggj.com/util/urlzpdf?url=" + jhjse)
            await dxpost(apiurl,{id:data[i].id,pdfUrl:pfds.data},"put")
            await download(pfds.data, data[i].fileName)
        }

    }
    loadinhg.value = false
}

</script>
<style scoped>
.kkmmxwerwxe {
    width: 810px;
    border: 0;
    height: 600px;
}

.sdfsdfdsrtrt {
    position: fixed;
    left: -10px;
    top: -10px;
    width: 1px;
    height: 1px;
    overflow: hidden;
}
</style>